using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

namespace LibraryAutomation
{
    class datalayer
    {
        //public static string default_NetLibDB_Projeler_ConnectionString = "Server=.\\SQLEXPRESS;Database=NetLibDB;User Id=sa;Password=1234";
        public static string default_NetLibDB_Projeler_ConnectionString = "Server=.\\SQL2005;Database=NetLibDB;User Id=sa;Password=unlu";
        //public static string default_NetLibDB_Projeler_ConnectionString = "Data Source=localhost\\SQLEXPRESS;Database=NetLibDB;Integrated Security=true";
        public static bool GetDataFromDB(string connectionString, string commandText, ref DataSet ds)
        {
            SqlConnection connection = new SqlConnection(connectionString);

            try
            {
                connection.Open();
                SqlCommand command = new SqlCommand();
                command.CommandTimeout = 0;
                command.Connection = connection;
                command.CommandText = commandText;
                command.CommandType = CommandType.Text;

                using (SqlDataAdapter da = new SqlDataAdapter(command))
                {
                    ds = new DataSet();
                    da.Fill(ds);
                    command.Parameters.Clear();
                    return true;

                }
            }
            catch (SqlException se)
            {
                MessageBox.Show(null, se.Message.ToString(), null, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }


        public static bool ParameterizedGetDataFromDB(string connectionString, SqlCommand cmd, ref DataSet ds)
        {
            SqlConnection connection = new SqlConnection(connectionString);

            try
            {
                connection.Open();
                cmd.CommandTimeout = 0;
                cmd.Connection = connection;
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    ds = new DataSet();
                    da.Fill(ds);
                    cmd.Parameters.Clear();
                    return true;

                }
            }
            catch (SqlException se)
            {
                MessageBox.Show(null, se.Message.ToString(), null, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }




        public static bool Execute(string connectionString, string commandText, ref int rowsAffected)
        {
            SqlConnection connection = null;

            try
            {
                connection = new SqlConnection(connectionString);
                connection.Open();

                SqlCommand command = new SqlCommand();
                command.CommandTimeout = 0;
                command.Connection = connection;
                command.CommandText = commandText;
                command.CommandType = CommandType.Text;
                rowsAffected = command.ExecuteNonQuery();
                return true;
            }
            catch (SqlException se)
            {
                MessageBox.Show(null, se.Message.ToString(), null, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }

        public static bool ParameterizedExecute(string connectionString, SqlCommand cmd, ref int rowsAffected)
        {
            SqlConnection connection = null;

            try
            {
                connection = new SqlConnection(connectionString);
                connection.Open();
                cmd.CommandTimeout = 0;
                cmd.Connection = connection;
                rowsAffected = cmd.ExecuteNonQuery();
                return true;
            }
            catch (SqlException se)
            {
                MessageBox.Show(null, se.Message.ToString(), null, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }

    
    
    }
}
